package com.eleadmin.store.biz.impl;

import com.eleadmin.damon.utils.constant.SqlConstant;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.PinyinUtils;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.store.biz.CustomerBiz;
import com.eleadmin.store.entity.Customer;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.store.result.SelectResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-02-10 22:23:45
 */
@Repository
public class CustomerBizImpl extends BaseDao implements CustomerBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Customer customer, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.pinyin");
        sql.append(",c.person,c.phone,c.tel,c.address");
        sql.append(",c.type,sdd.dict_data_name type_name, c.orderNum,c.create_time,c.create_user");
        sql.append(",c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=c.update_user ");

        sql.append(" left join (SELECT ta.* FROM sys_dictionary_data ta ");
        sql.append(" LEFT JOIN sys_dictionary tb ON ta.dict_id=tb.dict_id  AND tb.deleted=0 ");
        sql.append(" WHERE ta.deleted=0 AND tb.dict_code ='customer_type') sdd ON c.type = sdd.dict_data_code ");

        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(customer)) {
            if (Utils.isNotEmpty(customer.getName())) {
                sql.append(" and (instr(c.name,:name)>0 or instr(c.pinyin,:name)>0) ");
                map.put("name", customer.getName());
            }
            if (Utils.isNotEmpty(customer.getType())) {
                sql.append(" and sdd.dict_data_code=:dict_data_code ");
                map.put("dict_data_code", customer.getType());
            }
            if (Utils.isNotEmpty(customer.getSort()) && Utils.isNotEmpty(customer.getOrder())) {
                sql.append(" order by c." + customer.getSort() + " " + customer.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Customer> list(Customer customer, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.pinyin");
        sql.append(",c.person,c.phone,c.tel,c.address");
        sql.append(",c.type,sdd.dict_data_name type_name, c.orderNum,c.create_time,c.create_user");
        sql.append(",c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=c.update_user ");

        sql.append(" left join (SELECT ta.* FROM sys_dictionary_data ta ");
        sql.append(" LEFT JOIN sys_dictionary tb ON ta.dict_id=tb.dict_id  AND tb.deleted=0 ");
        sql.append(" WHERE ta.deleted=0 AND tb.dict_code ='customer_type') sdd ON c.type = sdd.dict_data_code ");

        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(customer)) {
            if (Utils.isNotEmpty(customer.getName())) {
                sql.append(" and (instr(c.name,:name)>0 or instr(c.pinyin,:name)>0) ");
                map.put("name", customer.getName());
            }
            if (Utils.isNotEmpty(customer.getType())) {
                sql.append(" and sdd.dict_data_code=:dict_data_code ");
                map.put("dict_data_code", customer.getType());
            }
            if (Utils.isNotEmpty(customer.getSort()) && Utils.isNotEmpty(customer.getOrder())) {
                sql.append(" order by c." + customer.getSort() + " " + customer.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSel(Customer customer, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select c.id value,c.name label");
        sql.append(" from we_customer c ");
        sql.append(" where c.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(customer)) {
            if (Utils.isNotEmpty(customer.getName())) {
                sql.append(" and (instr(c.name,:name)>0 or instr(c.pinyin,:name)>0)");
                map.put("name", customer.getName());
            }
            if (Utils.isNotEmpty(customer.getType())) {
                sql.append(" and instr(c.type,:type)>0");
                map.put("type", customer.getType());
            }
            if (Utils.isNotEmpty(customer.getSort()) && Utils.isNotEmpty(customer.getOrder())) {
                sql.append(" order by c." + customer.getSort() + " " + customer.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        sql.append(" limit "+ SqlConstant.LIMIT);

        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Customer getById(String id) {
        RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name, c.pinyin");
        sql.append(",c.person,c.phone,c.tel,c.address");
        sql.append(",c.type,sdd.dict_data_name type_name, c.orderNum,c.create_time,c.create_user");
        sql.append(",c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=c.update_user ");

        sql.append(" left join (SELECT ta.* FROM sys_dictionary_data ta ");
        sql.append(" LEFT JOIN sys_dictionary tb ON ta.dict_id=tb.dict_id  AND tb.deleted=0 ");
        sql.append(" WHERE ta.deleted=0 AND tb.dict_code ='customer_type') sdd ON c.type = sdd.dict_data_code ");

        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 and c.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean save(Customer customer, User loginUser) {
        customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into we_customer (");
        sql.append("organization_id,name,pinyin,person,phone");
        sql.append(",tel,address,type,orderNum");
        sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:pinyin,:person,:phone");
        sql.append(",:tel,:address,:type,:orderNum");
        sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", customer.getName());
        map.put("pinyin", customer.getPinyin());
        map.put("person", customer.getPerson());
        map.put("phone", customer.getPhone());
        map.put("tel", customer.getTel());
        map.put("address", customer.getAddress());
        map.put("type", customer.getType());
        map.put("orderNum", customer.getOrdernum() == null ? null : customer.getOrdernum() + "");
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Customer customer, User loginUser) {
        customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update we_customer set ");
        sql.append("name=:name,pinyin=:pinyin,person=:person,phone=:phone ");
        sql.append(",tel=:tel,address=:address,type=:type ");
        sql.append(",orderNum=:orderNum,update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", customer.getName());
        map.put("pinyin", customer.getPinyin());
        map.put("person", customer.getPerson());
        map.put("phone", customer.getPhone());
        map.put("tel", customer.getTel());
        map.put("address", customer.getAddress());
        map.put("type", customer.getType());
        map.put("orderNum", customer.getOrdernum() == null ? null : customer.getOrdernum() + "");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", customer.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean setPinyin(Customer customer, User loginUser) {
        customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update we_customer set ");
        sql.append("pinyin=:pinyin");
        sql.append(" where id=:id ");
        map.put("pinyin", customer.getPinyin());
        map.put("id", customer.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update we_customer set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
